package com.example.shop_2212.login

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.SpanUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.base.net.Const
import com.example.shop_2212.R
import com.example.shop_2212.databinding.ActivityLoginCodeBinding
import com.example.shop_2212.room.AppDBUtils
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
@Route(path = "/app/LoginCodeActivity")
class LoginCodeActivity : BaseActivity<ActivityLoginCodeBinding,LoginViewModel>() {

    override fun initView() {
        //登录按钮
       binding.btnLoginCode.setOnClickListener {
           val phone = binding.etUsername.text.toString()
           val code = binding.etCode.text.toString()
           if(phone.isEmpty()||code.isEmpty()){
               ToastUtils.showLong("手机号和验证码不能为空")
               return@setOnClickListener
           }
           if(!RegexUtils.isMobileSimple(phone)){
               ToastUtils.showLong("手机号格式不正确")
               return@setOnClickListener
           }
           if(code.length!=6){
               ToastUtils.showLong("验证码格式不正确")
               return@setOnClickListener
           }
           viewModel.loginCode(binding.etCode.text.toString(),
               binding.etUsername.text.toString())
       }
        //发送验证码按钮
        binding.btnSendCode.setOnClickListener {
            val phone = binding.etUsername.text.toString()//拿到手机号
            if(phone.isEmpty()){
                ToastUtils.showLong("手机号不能为空")
                return@setOnClickListener
            }
            if(!RegexUtils.isMobileSimple(phone)){
                ToastUtils.showLong("手机号格式不正确")
            }else{
                viewModel.sendCode(binding.etUsername.text.toString())
                /**
                 * 点击获取验证码后，验证码不可再次获取，60秒可在使用 ----可使用timer （可使用downTo）
                 */
                binding.btnSendCode.isEnabled = false//按钮不可用
                //binding.btnSendCode.isVisible = false//按钮是否能看见
                lifecycleScope.launch {
                    for (i in 10 downTo 0){
                        delay(1000)
                        binding.btnSendCode.text = "$i 秒后再次获取"
                    }
                    binding.btnSendCode.text = "获取验证码"
                    binding.btnSendCode.isEnabled = true//可用
                }
            }
        }
        //富文本
        val builder = SpannableStringBuilder(binding.fwb.text.toString())
        builder.setSpan(ForegroundColorSpan(Color.RED),2,4,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)//文字颜色
        val clickableSpan = object :ClickableSpan(){
            override fun onClick(widget: View) {
                ARouter.getInstance().build("/app/WebViewActivity").withString("url","http://www.baidu.com").navigation()
            }
        }
        builder.setSpan(clickableSpan,6,12,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)//点击事件
        val clickableSpan1 = object :ClickableSpan(){
            override fun onClick(widget: View) {
                ARouter.getInstance().build("/app/WebViewActivity").withString("url","http://www.sina.com").navigation()
            }
        }
        builder.setSpan(clickableSpan1,13,19,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)//点击事件
        binding.fwb.movementMethod = LinkMovementMethod.getInstance()//一定添加这行，不然富文本点击不起作用
        binding.fwb.text = builder

        //来回切换登录方式
        binding.btnGologin.setOnClickListener {
            finish()
            ARouter.getInstance().build("/app/LoginActivity").navigation()
        }

    }

    override fun initData() {
        viewModel.loginCodeSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                SPUtils.getInstance().put(Const.SP_TOKEN,it.data.token)
                SPUtils.getInstance().put("phone",it.data.phone)
                SPUtils.getInstance().put("uid",it.data.uid)
                //保存用户信息到数据库
                AppDBUtils.getDB().getUserDao().insert(it.data)
                ARouter.getInstance().build("/app/ShopActivity").navigation()
            }

        }
        viewModel.loginCodeFail.observe(this){
            ToastUtils.showLong(it)
        }
        viewModel.codeSuccess.observe(this){
            if(it.code==Const.RESULT_OK){
                binding.etCode.setText(it.data.content.substring(8,14))//将验证码显示到et上
            }
        }
        viewModel.codeFail.observe(this){
            ToastUtils.showLong(it)
        }
    }

    override fun getViewModel(): Class<LoginViewModel> {
        return LoginViewModel::class.java
    }

    override fun getLayout() = R.layout.activity_login_code

}